import Marquee from "./components/Marquee";
import ConfirmPopup from "./components/popups/ConfirmPopup"
import BaseScene from "./core/BaseScene";
import ResManager from "./core/ResManager";
import { HttpRequest } from "./net/HttpRequest";
import LogUtil from "./utils/LogUtil";
import { Utils } from "./utils/Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class test extends BaseScene {

    @property(Marquee)
    marquee: Marquee = null

    @property(cc.Prefab)
    popBox: cc.Prefab = null

    popCtrl = null

    socket = null;

    onLoad() {
        super.onLoad();
        LogUtil.log(LogUtil.getFileName(),'test111111111')
        let teststr = '攻击增加{0}，血量增加{1}';
        LogUtil.log(LogUtil.getFileName(),teststr.format([100, 2000]));

        // let obj = {
        //     resourceId: "config_language_zh",
        //     resourcePath: 'config/language_zh',
        //     resourceType: 'csv',
        //     key: ["languageId"],
        // }
        // ResManager.ins().load(obj,(res)=>{
        //     LogUtil.log(LogUtil.getFileName(),'资源加载成功',res);
        // });

        this.AsyncGetConfig();

        let test1 = {
            code:1,
            msg:'11111'
        }
        LogUtil.log(LogUtil.getFileName(),'test11======',Utils.isJson(test1));
        LogUtil.log(LogUtil.getFileName(),'test22======',Utils.JsonDeepCopy(test1));
        
    }

    async AsyncGetConfig() {
        let obj = {
            resourceId: "config_language_zh",
            resourcePath: 'config/language_zh',
            resourceType: 'csv',
            key: ["languageId"],
        }
        let _config = await ResManager.ins().getResAsync(obj);
        LogUtil.log(LogUtil.getFileName(),_config);
        LogUtil.log(LogUtil.getFileName(),_config['sign01'].text.format([10]));
    }

    start() {
        this.marquee.push(['大家好，我是小明', '欢迎欢迎欢迎欢迎欢迎！']);
        this.marquee.play();

        // var socket = io('http://127.0.0.1:3000');
        // socket.on('connected', () => {
        //     LogUtil.log(LogUtil.getFileName(),'Socket connected');
        // });

        // let httpPromise = HttpRequest.ins().httpPromise(':3000', { username: 'aaa', password: 'password' }, 'get', () => {
        //     LogUtil.log(LogUtil.getFileName(),'错误处理1');
        // })
        // httpPromise.then((data) => {
        //     cc.log('1111111111',data);
        //     var socket = io();
        //     socket.on('connected', () => {
        //         LogUtil.log(LogUtil.getFileName(),'Socket connected');
        //     });
        // }).catch(() => {
        //     LogUtil.log(LogUtil.getFileName(),'错误处理2');
        // })
    }

    btnClick() {
        if (this.popCtrl) {
            this.popCtrl.show()
        } else {
            let node = cc.instantiate(this.popBox);
            node.parent = this.node;
            this.popCtrl = node.getComponent(ConfirmPopup);
            this.popCtrl.show({
                title: '提示',
                content: '弹窗测试',
                confirmCallback: () => {
                    LogUtil.log(LogUtil.getFileName(),'xxxx');
                }
            });
        }
    }

    btnLink(){
        let httpPromise = HttpRequest.ins().httpPromise(':3000/getServerInfo', { username: 'aaa', password: 'password' }, 'get', () => {
            LogUtil.log(LogUtil.getFileName(),'错误处理1');
        })
        httpPromise.then((data) => {
            LogUtil.log(LogUtil.getFileName(),'1111111111',data);
            // var socket = io('http://127.0.0.1:3000');
            // this.socket = socket;
            // socket.on('connected', () => {
            //     LogUtil.log(LogUtil.getFileName(),'Socket connected');
            // });
        }).catch(() => {
            LogUtil.log(LogUtil.getFileName(),'错误处理2');
        })
    }

    btnSend(){
        if(this.socket){
            LogUtil.log(LogUtil.getFileName(),'发送');
            this.socket.emit('login.LoginReq', 'hello');
        }
    }

}
